<!doctype html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>NodeCMS首页</title>
    <link rel="stylesheet" href="/css/layui/css/layui.css">
    <link rel="stylesheet" href="/css/admin/reset.css">
    <link rel="stylesheet" href="/css/admin/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <!-- 公共头部 -->
    <%- include('../common/header') %>
    <!-- 公共头部 -->
    <!-- 导航条 -->
    <%- include('../common/nav') %>
    <!-- 导航条 -->
    <div class="layui-body">
        <div class="container">
            <div class="layui-breadcrumb">
                <a href="/">首页</a>
                <a href="javascript:;">用户列表</a>
            </div>
            <div class="action-box">
                <div class="float-left layui-form" lay-filter="searchItem">
                    <div class="layui-form-item">
                        <div class="layui-inline action-search">
                            <input type="text" name="username" value="<%= findUserName %>" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline">
                            <a href="javascript:;" id="searchBtn">搜索</a>
                        </div>
                    </div>
                </div>
                <div class="float-right">
                    <a href="/admin/addUser" class="layui-btn layui-btn-normal">添加人员</a>
                    <button type="button" class="layui-btn layui-btn-danger">批量删除</button>
                </div>
            </div>
            <div class="layui-form">
                <table class="layui-table">
                    <colgroup>
                        <col width="60">
                        <col width="70">
                        <col width="100">
                        <col width="200">
                        <col width="250">
                        <col>
                        <col width="200">
                    </colgroup>
                    <thead>
                    <tr>
                        <th>选择</th>
                        <th class="text-center">ID</th>
                        <th>用户名</th>
                        <th>身份</th>
                        <th>创建时间</th>
                        <th>权限状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                        <% for(var i=0;i<results.userList.length;i++){ %>
                        <tr>
                            <td align="center"><input type="checkbox" name="" lay-skin="primary"></td>
                            <td align="center"><%= results.userList[i].id %></td>
                            <td><a href="<% if(results.userList[i].id != userInfo.userId){ %>/admin/updateUser?id=<%= results.userList[i].id %><% }else{ %>javascript:;<% } %>"><%= results.userList[i].username %></a></td>
                            <td><%= results.userList[i].usertype == 1 ? '超级管理员' : '管理员' %></td>
                            <td><%= timeFormat(results.userList[i].createtime) %></td>
                            <td class="user-authority">
                                <% if(results.userList[i].newsAuthority){ %>
                                <span>
                                    <% if(results.userList[i].newsAuthority == 1){ %>
                                    管理资讯
                                    <% }else if(results.userList[i].newsAuthority == 2){ %>
                                    只读资讯
                                    <% } %>
                                </span>
                                <% } %>
                                <% if(results.userList[i].flowerAuthority){ %>
                                <span>
                                    <% if(results.userList[i].flowerAuthority == 1){ %>
                                    花语权限
                                    <% } %>
                                </span>
                                <% } %>
                            </td>
                            <td>
                                <% if(userInfo.userType == 1 && results.userList[i].id != userInfo.userId){ %><a class="removeItem" href="javascript:;" data-id="<%= results.userList[i].id %>" data-name="<%= results.userList[i].username %>">删除</a><% } %>
                            </td>
                        </tr>
                        <% } %>
                    </tbody>
                </table>
            </div>
            <div class="pagebox" id="pagebox"><%- laypageHtml %></div>
        </div>
    </div>
</div>
<%- include('../common/importJs') %>
<script>
    function getSearchParams(url){
        var url = url;
        var search = url.split('?').length >= 2 ? url.split('?')[1] : '';
        var json = {};
        if(search){
            var arr = search.split('&');
            $.each(arr, function(){
                var keyArr = this.split('=');
                json[keyArr[0]] = keyArr[1];
            })
        }
        return json;
    }
    layui.use(['element', 'form', 'laypage'], function(){
        var element = layui.element;
        var form = layui.form;
        var laypage = layui.laypage;
        var total = $('#pageTotal').val();
        var query = getSearchParams(window.location.href);
        $('.removeItem').on('click', function(){
            var id = $(this).attr('data-id');
            var name = $(this).attr('data-name');
            var checkHandle = confirm('确认删除' + name + '吗？');
            if (checkHandle == true){
                $.ajax({
                    url: '/cms/removeUserById',
                    type: 'post',
                    data: {
                        id: id
                    },
                    success: function(d){
                        if(d.code == 200){
                            window.location.href = '/admin/userList';
                        }
                    }
                })
            }
        })
        $('#searchBtn').on('click', function(){
            var data = form.val('searchItem');
            var url = window.location.href.replace(/\?\S+/,'');
            url += '?username=' + data.username;
            window.location.href = url;
        });
    });
</script>
</body>
</html>